//
//  MovieListViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "MovieDetailViewController.h"
#import "AppTools.h"
#import "MovieList.h"
#import "MovieListDB.h"
@interface MovieListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollV;
@property(nonatomic,assign)BOOL isUpLoad;

@end

@implementation MovieListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.allMovies = [NSMutableArray array];
    self.isUpLoad = NO;
    
    [[MovieListDB shareDataBase]openDB];
    self.allMovies = [[MovieListDB shareDataBase]selectSutdent];
//    NSLog(@"%@",self.allMovies);
   double height =([[UIScreen mainScreen] bounds].size.height-65-49);
    self.movieTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 376, height) style:UITableViewStylePlain];
    [self.view addSubview:self.movieTableView];
    [_movieTableView release];
    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    [self.movieTableView addHeaderWithCallback:^{
        self.isUpLoad = NO;
        [self loadData];
    }];
    [self.movieTableView addFooterWithCallback:^{
        self.isUpLoad = YES;
        [self loadData];
    }];
    
    [self.movieTableView headerBeginRefreshing];
    
    [self.movieTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"movie"];
}

-(void)loadData{
    NSString *url = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    [AppTools getLocalDataWithURL:url andBlock:^(id dic) {
        NSMutableArray *array = [dic objectForKey:@"result"];
        if (self.isUpLoad == NO) {
            NSLog(@"下拉刷新");
            [self.allMovies removeAllObjects];
        }else{
            NSLog(@"上啦刷新");
        }
        [[MovieListDB shareDataBase]dropTable];
        for (NSMutableDictionary *dict in array) {
            MovieList *movieList = [[MovieList alloc]init];
            [movieList setValuesForKeysWithDictionary:dict];
            [self.allMovies addObject:movieList];
            [movieList release];
        }
        [self saveToLocal:(self.allMovies)];
        [self.movieTableView headerEndRefreshing];
        [self.movieTableView footerEndRefreshing];
        [self.movieTableView reloadData];
    }];
}
-(void)saveToLocal:(NSMutableArray*)array{
    NSLog(@"save!!!!!!%ld",array.count);
    [[MovieListDB shareDataBase]openDB];
    [[MovieListDB shareDataBase]createTable];
    for (int i = 0; i < array.count; i++) {
        MovieList *movie = [array objectAtIndex:i];
        [[MovieListDB shareDataBase]insertStudent:movie];
    }
}

-(void)GoToMovieDetail:(NSString *)string{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
            movieDetailVC.movieID = string;
            [self.navigationController pushViewController:movieDetailVC animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allMovies.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indetifer = @"movie";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifer];
    cell.movie = [self.allMovies objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc]init];
    MovieList *movie = [self.allMovies objectAtIndex:indexPath.row];
    movieDetailVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDetailVC animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 190;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
